package com.zxy.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zxy.constant.MessageConstant;
import com.zxy.entity.Result;
import com.zxy.pojo.OrderSetting;
import com.zxy.service.OrderSettingService;
import com.zxy.util.POIUtils;
import org.aspectj.bridge.Message;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {
    @Reference
    OrderSettingService orderSettingService;

    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
           orderSettingService.editNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        try {
            // 使用工具类读取数据到list集合
            List<String[]> list = POIUtils.readExcel(excelFile);
            // 数据类型转换
            ArrayList<OrderSetting> listData = new ArrayList<>();
            for (String[] strings : list) {                 //   List<String[]> list    Sting[]是类型不是名称
                String dateStr = strings[0];  // 数据是横向存储进去的  Strings01 =(Date)2020/12/01, (number)200
                String numberStr = strings[1];               //     Strings02 = (Date)2020/12/01, (number)100
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(new Date(dateStr));
                orderSetting.setNumber(Integer.parseInt(numberStr));
                orderSetting.setReservations(0);
                listData.add(orderSetting);
            }
            orderSettingService.addBatch(listData);
            return new Result(true, MessageConstant.UPLOAD_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPLOAD_FAIL);
        }
    }
}
